package com.laikan.reader.apirequester;

import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.core.events.LaiKanEvent;
import com.socks.library.KLog;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by anrun on 9/28/16.
 */
public class FindPwdRequester {

    /**
     * 1步 - 获取验证码
     */
    public static void getYz(String phone_number, String deviceId) {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("deviceId", deviceId);
        map.put("mobile", phone_number);
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                try {
                    JSONObject obj = new JSONObject(httpResponse.responseContent);
                    String result = obj.getString("result");
                    if (Constants.RESULT_OK.equals(result)) {
                        ToastMgr.showShortToast(R.string.login_yanzheng_send);
                        LaiKanEvent event = LaiKanEvent.getInstance();
                        event.setEventID(LaiKanEvent.EVENT_ID_GET_VERIFY_CODE);
                        LaikanApplication.postEvent(event);
                    }else {
                        ToastMgr.showShortToast(obj.getString("error_msg"));
                    }
                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("网络不给力，请重试。20");

            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL() + "accounts/mobile/forget");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL() + "accounts/mobile/forget", map, null, null, requestListener);
    }

    /**
     * 2步 - 提交验证
     */
    public static void checkVerifyCode(String verify_code, String phone_number) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("code", verify_code);
        map.put("mobile", phone_number);
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                try {
                    JSONObject obj = new JSONObject(httpResponse.responseContent);
                    String result = obj.getString("result");
                    if (Constants.RESULT_OK.equals(result)) {
                        LaiKanEvent event = LaiKanEvent.getInstance();
                        event.setEventID(LaiKanEvent.Event_ID_CHECK_VERIFY_CODE);
                        LaikanApplication.postEvent(event);
                    }else {
                        ToastMgr.showShortToast(obj.getString("error_msg"));
                    }
                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("网络不给力，请重试。19");
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL() + "accounts/check_code");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL() + "accounts/check_code", map, null, null, requestListener);
    }

    /**
     * 3步 - 修改密码
     */
    //TODO 处理忘记密码的接口
    public static void forget(String password, String password_confirm, String phone_number, String verify_code) {

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("password", password);
        map.put("passwordRepeat", password_confirm);
        map.put("mobile", phone_number);
        map.put("code", verify_code);
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                try {
                    JSONObject obj = new JSONObject(httpResponse.responseContent);
                    String result = obj.getString("result");
                    if (Constants.RESULT_OK.equals(result)) {
                        ToastMgr.showShortToast("密码重置成功");
                        LaiKanEvent event = LaiKanEvent.getInstance();
                        event.setEventID(LaiKanEvent.EVENT_ID_RESET_PASSWORD);
                        LaikanApplication.postEvent(event);
                    }else {
                        ToastMgr.showShortToast(obj.getString("error_msg"));
                    }
                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("网络不给力，请重试。21");
            }

            @Override
            public void onFinish() {

            }
        };

        Requester requester = new Requester();
        requester.setUrl(APIProtocol.getRootURL() + "/accounts/resetpwd");
        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpPostRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpPostRequest(APIProtocol.getRootURL() + "/accounts/resetpwd", map, null, null, requestListener);
    }
}
